package com.lc.zpyh.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.CancelFavoriteApi;
import com.lc.zpyh.http.request.FavoritesListApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.response.FavoritesListBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.supermarket.BusinessActivity;
import com.lc.zpyh.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CollectionActivity extends AppActivity {
    @BindView(R.id.img_typetwo)
    ImageView imgTypetwo;
    @BindView(R.id.img_typeone)
    ImageView imgTypeone;
    @BindView(R.id.rv_collection)
    RecyclerView rvCollection;
    List<FavoritesListBean.ListBean> favoritesList = new ArrayList<>();
    @BindView(R.id.rl_one)
    RelativeLayout rlOne;
    @BindView(R.id.rv_collection_two)
    RecyclerView rvCollectionTwo;
    @BindView(R.id.rl_two)
    RelativeLayout rlTwo;
    private BaseQuickAdapter<FavoritesListBean.ListBean, BaseViewHolder> adapter;
    private BaseQuickAdapter<FavoritesListBean.ListBean, BaseViewHolder> twoadapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        favoritesList = (List<FavoritesListBean.ListBean>) getIntent().getSerializableExtra("favoritesList");
        if (favoritesList != null) {
            rlOne.setVisibility(View.GONE);
            rlTwo.setVisibility(View.VISIBLE);
            setTwoAdapter();
        }


    }

    //取消收藏
    private void cancelFavorite(String commodityId, String type) {
        EasyHttp.post(this)
                .api(new CancelFavoriteApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, "")).setCommodityId(commodityId)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode() == 0) {
                    toast("已取消收藏");
                    favoritesList(type);


                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }


    private void setOneAdapter() {
        if (adapter == null) {
            rvCollection.setLayoutManager(new GridLayoutManager(this, 2));

            adapter = new BaseQuickAdapter<FavoritesListBean.ListBean, BaseViewHolder>
                    (R.layout.item_collection_one, favoritesList) {

                @Override
                protected void convert(BaseViewHolder baseViewHolder, FavoritesListBean.ListBean dataBean) {
                    GlideApp.with(CollectionActivity.this)
                            .load(IntentKey.CAR_IMAGE+dataBean.getImg())
                            .into((ImageView) baseViewHolder.getView(R.id.img_shop));
                    baseViewHolder.setText(R.id.tv_goods_name, dataBean.getCommodityName());
                    baseViewHolder.setText(R.id.tv_shop_name, dataBean.getMerchantName());
                    baseViewHolder.setText(R.id.tv_xiaoliang, dataBean.getCanteenName());
                    baseViewHolder.getView(R.id.img_cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelFavorite(dataBean.getCommodityId() + "", "1");
                        }
                    });
                }


            };


            View emptyview = View.inflate(CollectionActivity.this, R.layout.empty_emptylayout, null);
             adapter.setOnItemClickListener(new OnItemClickListener() {
                 @Override
                 public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                     startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id",favoritesList.get(position).getMerchantId()+""));
                 }
             });
            adapter.setEmptyView(emptyview);
            rvCollection.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }

    }

    private void setTwoAdapter() {
        if (twoadapter == null) {
            rvCollectionTwo.setLayoutManager(new LinearLayoutManager(this));
            twoadapter = new BaseQuickAdapter<FavoritesListBean.ListBean, BaseViewHolder>
                    (R.layout.item_collection_two, favoritesList) {

                @Override
                protected void convert(BaseViewHolder baseViewHolder, FavoritesListBean.ListBean dataBean) {
                    GlideApp.with(CollectionActivity.this)
                            .load(IntentKey.CAR_IMAGE+dataBean.getImg())
                            .into((ImageView) baseViewHolder.getView(R.id.img_shop));
                    baseViewHolder.setText(R.id.tv_goods_name, dataBean.getCommodityName());
                    baseViewHolder.setText(R.id.tv_shop_name, dataBean.getMerchantName());
                    baseViewHolder.setText(R.id.tv_xiaoliang, dataBean.getCanteenName());
                    baseViewHolder.getView(R.id.img_cancel).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelFavorite(dataBean.getCommodityId() + "", "2");
                        }
                    });
                }


            };

             twoadapter.setOnItemClickListener(new OnItemClickListener() {
                 @Override
                 public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                     startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id",favoritesList.get(position).getMerchantId()+""));
                 }
             });
            rvCollectionTwo.setAdapter(twoadapter);
        } else {
            twoadapter.notifyDataSetChanged();
        }
    }

    /**
     * 我的收藏列表
     */
    private void favoritesList(String type) {
        EasyHttp.post(this)
                .api(new FavoritesListApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, "")).setPageNum("1").setPageSize("999")).request(new HttpCallback<FavoritesListBean>(this) {

            @Override
            public void onSucceed(FavoritesListBean data) {
                if (data.getCode() == 0) {

                    favoritesList.clear();
                    favoritesList.addAll(data.getList());
                    if (type.equals("1")) {
                        setOneAdapter();
                    } else {
                        setTwoAdapter();
                    }
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @OnClick({R.id.image_back, R.id.img_typetwo, R.id.img_typeone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.img_typetwo:
                rlOne.setVisibility(View.VISIBLE);
                rlTwo.setVisibility(View.GONE);
                setOneAdapter();
                break;
            case R.id.img_typeone:
                rlOne.setVisibility(View.GONE);
                rlTwo.setVisibility(View.VISIBLE);
                setTwoAdapter();


                break;
        }
    }
}
